// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/base/api/AppMenuApi'

import TreeClass from '@api/base/model/AppMenuTreeReq'
import AddClass from '@api/base/model/AppMenuReq'
import DelClass from '@api/base/model/AppMenuDeleteReq'
import BaseClass from '@api/base/model/AppMenuDto'

const EditClass = AddClass

const CLIENT = CORE.grpcCLIENT(Client)
const BASE = [BaseClass, 'dto']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 结构树 —————————————————————————————— //

  async getTree(treeParam, TIP = '查询结构树') {
    const DTO = CORE.grpcDTO(TreeClass, treeParam)
    const resp = await CORE.grpc(TIP, CLIENT, 'appMenuTreeNoPri', DTO)
    return resp.dto || []
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  transAdd(one) {
    const forword = one.forword || 'none'
    const jumpLink = one.jumpLink || 'none'
    return { ...one, forword, jumpLink }
  },
  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'appMenuSave', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  transEditToAdd: true,
  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'appMenuUpdate', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: [id] })
    await CORE.grpc(TIP, CLIENT, 'appMenuDelete', DTO)
    return CORE.grpcOK(TIP)
  },
}
